package com.sls.web.manager.action.game;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;

import com.sls.web.manager.action.SuperAction;
import com.sls.web.manager.common.Constants;
import com.sls.web.manager.common.FilterStringSpaces;
import com.sls.web.manager.execption.ManagerException;
import com.sls.web.manager.game.pojo.OhFly;
import com.sls.web.manager.game.pojo.OhFlyUserInfo;
import com.sls.web.manager.game.querybean.OhFeiGoodsAreaServerQuery;
import com.sls.web.manager.game.querybean.OhFlyQuery;
import com.sls.web.manager.game.service.OhFlyService;
import com.sls.web.manager.page.Pagination;

public class OhFlyAction extends SuperAction {

	private static final long serialVersionUID = 1L;
	@Resource(name = "ohFlyService")
	private OhFlyService ohFlyService;
	private String goodsId;
	private String suppliersId;
	private String slsGoodsId;
	private OhFly ohFly;
	private OhFlyQuery ohFlyQuery;
	private Pagination<?> page;
	private String userid;
	private String userpws;
	private OhFeiGoodsAreaServerQuery ohFeiGoodsAreaServerQuery;
	private OhFlyUserInfo ohFlyUserInfo;
	private String gameId;
	private List<OhFlyUserInfo> users;
	private int priceRatio;
	private String supplierId;

	/**
	 * 列表
	 * 
	 * @return
	 * @throws Exception
	 */
	public String list() throws Exception {
		try {
			if (ohFlyQuery == null) {
				ohFlyQuery = new OhFlyQuery();
				ohFlyQuery.setPageSize(Constants.INIT_PAGESIZE);
				ohFlyQuery.setCurrentPage(Constants.INIT_CURRENTPAGE);
			}
			String goodsId = ohFlyQuery.getGoodsId();
			String goodsName = ohFlyQuery.getGoodsName();
			goodsId = FilterStringSpaces.checkStringHasSpace(goodsId);
			goodsName = FilterStringSpaces.checkStringHasSpace(goodsName);
			ohFlyQuery.setGoodsId(goodsId);
			ohFlyQuery.setGoodsName(goodsName);
			page = ohFlyService.queryOhFly(ohFlyQuery, getCurrentOperator());
		} catch (ManagerException e) {
			addActionError(e.getMessage());
		}
		return LIST;
	}

	/**
	 * 详细信息
	 * 
	 * @return
	 * @throws Exception
	 */
	public String detail() throws Exception {
		try {
			ohFly = ohFlyService.queryDetailById(goodsId, getCurrentOperator());
			if (ohFly == null) {
				return ERROR;
			}
		} catch (ManagerException e) {
			return ERROR;
		}
		return "detail";
	}

	/**
	 * 用户输入界面
	 * 
	 * @throws Exception
	 */
	public String input() throws Exception {
		users = ohFlyService.queryOhFlyUserInfo(getCurrentOperator());
		return "userinfo_input";
	}

	/**
	 * 用户查询信息
	 * 
	 * @return
	 * @throws Exception
	 */
	public String queryUserInfo() throws Exception {
		try {
			List<OhFlyUserInfo> userList = ohFlyService
					.queryOhFlyUserInfo(getCurrentOperator());
			String username = ohFlyUserInfo.getUserid();
			if (StringUtils.isNotEmpty(username)) {
				username = username.trim();
			}
			String userpws = null;
			if (userList != null) {
				for (OhFlyUserInfo user : userList) {
					String userid = user.getUserid();
					if (StringUtils.isNotEmpty(userid)) {
						userid = userid.trim();
					}
					if (username.equals(userid)) {
						userpws = user.getUserpws();
						if (StringUtils.isNotEmpty(userpws)) {
							userpws = userpws.trim();
						}
						break;
					}
				}

			}
			ohFlyUserInfo = ohFlyService.queryUerInfo(username, userpws,
					getCurrentOperator());
			if (ohFlyUserInfo != null
					&& !"1".equals(ohFlyUserInfo.getRetcode())) {
				if (StringUtils.isNotEmpty(ohFlyUserInfo.getErr_msg())) {
					flag = false;
					addActionMessage(ohFlyUserInfo.getErr_msg());
				}
			} else {
				users = ohFlyService.queryOhFlyUserInfo(getCurrentOperator());
				return "userinfo_input";
			}
		} catch (ManagerException e) {
			flag = false;
			addActionError(e.getMessage());
		}
		return "sync_success";
	}

	/**
	 * 游戏区服查询
	 * 
	 * @return
	 * @throws Exception
	 */
	public String listGame() throws Exception {
		try {
			if (ohFeiGoodsAreaServerQuery == null) {
				ohFeiGoodsAreaServerQuery = new OhFeiGoodsAreaServerQuery();
				ohFeiGoodsAreaServerQuery.setPageSize(Constants.INIT_PAGESIZE);
				ohFeiGoodsAreaServerQuery
						.setCurrentPage(Constants.INIT_CURRENTPAGE);
			}
			String gameId = ohFeiGoodsAreaServerQuery.getGameId();
			String gameName = ohFeiGoodsAreaServerQuery.getGameName();
			gameId = FilterStringSpaces.checkStringHasSpace(gameId);
			gameName = FilterStringSpaces.checkStringHasSpace(gameName);
			ohFeiGoodsAreaServerQuery.setGameId(gameId);
			ohFeiGoodsAreaServerQuery.setGameName(gameName);
			page = ohFlyService.queryOhFlyGameArea(ohFeiGoodsAreaServerQuery,
					getCurrentOperator());
			if (page == null) {
				return ERROR;
			}
		} catch (ManagerException e) {
			return ERROR;
		}
		return "game_list";
	}

	/**
	 * 殴飞游戏区服同步
	 * 
	 * @return
	 * @throws Exception
	 */
	public String gameSync() throws Exception {
		try {
			int syncCount = ohFlyService.ohFlyGameAreaSync(gameId.trim(),
					getCurrentOperator());
			addActionMessage(String.valueOf(syncCount));
			redirectionUrl = "oh_fly!listGame.htm";
		} catch (ManagerException e) {
			flag = false;
			addActionError(e.getMessage());
		}
		return "sync_success";

	}

	/**
	 * 殴飞商品审核
	 * 
	 * @return
	 * @throws Exception
	 */
	public String audit() throws Exception {
		try {
			this.ohFlyService.confirmAudit(supplierId, priceRatio,
					getCurrentOperator());
			redirectionUrl = "oh_fly!list.htm";
		} catch (ManagerException e) {
			flag = false;
			addActionError(e.getMessage());
		}
		return "audit_success";
	}

	/**
	 * 批量上架
	 * 
	 * @return
	 * @throws Exception
	 */
	public String batchOnline() throws Exception {
		try {
			this.ohFlyService.batchOnline("1", getCurrentOperator());
			redirectionUrl = "oh_fly!list.htm";
		} catch (ManagerException e) {
			flag = false;
			addActionError(e.getMessage());
		}
		return "audit_success";
	}

	/**
	 * 批量下架
	 * 
	 * @return
	 * @throws Exception
	 */
	public String batchOffline() throws Exception {
		try {
			this.ohFlyService.batchOffline("0", getCurrentOperator());
			redirectionUrl = "oh_fly!list.htm";
		} catch (ManagerException e) {
			flag = false;
			addActionError(e.getMessage());
		}
		return "audit_success";
	}

	public OhFlyService getOhFlyService() {
		return ohFlyService;
	}

	public void setOhFlyService(OhFlyService ohFlyService) {
		this.ohFlyService = ohFlyService;
	}

	public String getGoodsId() {
		return goodsId;
	}

	public void setGoodsId(String goodsId) {
		this.goodsId = goodsId;
	}

	public String getSuppliersId() {
		return suppliersId;
	}

	public void setSuppliersId(String suppliersId) {
		this.suppliersId = suppliersId;
	}

	public String getSlsGoodsId() {
		return slsGoodsId;
	}

	public void setSlsGoodsId(String slsGoodsId) {
		this.slsGoodsId = slsGoodsId;
	}

	public OhFly getOhFly() {
		return ohFly;
	}

	public void setOhFly(OhFly ohFly) {
		this.ohFly = ohFly;
	}

	public OhFlyQuery getOhFlyQuery() {
		return ohFlyQuery;
	}

	public void setOhFlyQuery(OhFlyQuery ohFlyQuery) {
		this.ohFlyQuery = ohFlyQuery;
	}

	public Pagination<?> getPage() {
		return page;
	}

	public void setPage(Pagination<?> page) {
		this.page = page;
	}

	public String getUserid() {
		return userid;
	}

	public void setUserid(String userid) {
		this.userid = userid;
	}

	public String getUserpws() {
		return userpws;
	}

	public void setUserpws(String userpws) {
		this.userpws = userpws;
	}

	public OhFlyUserInfo getOhFlyUserInfo() {
		return ohFlyUserInfo;
	}

	public void setOhFlyUserInfo(OhFlyUserInfo ohFlyUserInfo) {
		this.ohFlyUserInfo = ohFlyUserInfo;
	}

	public OhFeiGoodsAreaServerQuery getOhFeiGoodsAreaServerQuery() {
		return ohFeiGoodsAreaServerQuery;
	}

	public void setOhFeiGoodsAreaServerQuery(
			OhFeiGoodsAreaServerQuery ohFeiGoodsAreaServerQuery) {
		this.ohFeiGoodsAreaServerQuery = ohFeiGoodsAreaServerQuery;
	}

	public String getGameId() {
		return gameId;
	}

	public void setGameId(String gameId) {
		this.gameId = gameId;
	}

	public List<OhFlyUserInfo> getUsers() {
		return users;
	}

	public void setUsers(List<OhFlyUserInfo> users) {
		this.users = users;
	}

	public int getPriceRatio() {
		return priceRatio;
	}

	public void setPriceRatio(int priceRatio) {
		this.priceRatio = priceRatio;
	}

	public String getSupplierId() {
		return supplierId;
	}

	public void setSupplierId(String supplierId) {
		this.supplierId = supplierId;
	}

}
